<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" ref="nav" @titlesClick="titlesClick"/>
    <scroll class="content" ref="scroll" @scroll="contentScroll" :probe-type="3">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detail-info="detailInfo" @imgLoad="imgLoad"/>
      <detail-param-info ref="param" :param-info="paramInfo"/>
      <detail-comment-info ref="comment" :comment-info="commentInfo"/>
      <goods-list ref="recommends" :goods="recommends" />
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"/>
    <back-top class="back-top" @click.native="backClick" v-show="showBackTop">
      <img src="~assets/img/common/top.png" alt="">
    </back-top> 
  </div>
</template>

<script>
import DetailNavBar from 'views/detail/childCpns/DetailNavBar'     
import DetailSwiper from 'views/detail/childCpns/DetailSwiper'   
import DetailBaseInfo from 'views/detail/childCpns/DetailBaseInfo'   
import DetailShopInfo from 'views/detail/childCpns/DetailShopInfo'   
import Scroll from 'components/common/scroll/Scroll'
import DetailGoodsInfo from 'views/detail/childCpns/DetailGoodsInfo' 
import DetailParamInfo from 'views/detail/childCpns/DetailParamInfo' 
import DetailCommentInfo from 'views/detail/childCpns/DetailCommentInfo' 
import GoodsList from 'components/content/goods/GoodsList'
import DetailBottomBar from 'views/detail/childCpns/DetailBottomBar' 
import BackTop from 'components/content/backTop/BackTop'

import {backTopMixin} from "common/mixin.js"

import {getDetail,getRecommend,Goods,Shop,GoodsParam} from 'network/detail.js'   
export default {
  name: 'Detail',
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTops: [],
      currentIndex: 0
    }
  },
  created() {
    this.getD_Detail();
    this.getD_Recommend();

  },
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    BackTop
  },
  mixins: [backTopMixin],
  methods: {
    /* 等待图片全部加载完成，进行刷新 */
    imgLoad() {
      this.$refs.scroll.refresh()
      this.getThemeTops()
    },
    addToCart() {
      // 1.创建对象
      const product = {}
      // 2.对象信息
      product.iid = this.iid;
      product.imgURL = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc;
      product.newPrice = this.goods.nowPrice;
      // 3.添加到Store中
      this.$store.dispatch('addCart', product)

    },
    getThemeTops() {
      this.themeTops = []
      this.themeTops.push(0)
      this.themeTops.push(this.$refs.param.$el.offsetTop)
      this.themeTops.push(this.$refs.comment.$el.offsetTop)
      this.themeTops.push(this.$refs.recommends.$el.offsetTop)
      this.themeTops.push(Number.MAX_VALUE)
    },
    /* 点击信息跳转 */
    titlesClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTops[index], 250)
    },
    contentScroll(position) {
      /* 获取y值 */
      const positionY = -position.y
      //返回顶部
      this.showBackTop = positionY > 500
      let length = this.themeTops.length;
      for (let i = 0; i < length-1; i++) {
      /**
       *  方案一:
       *    条件: (i < (length-1) && currentPos >= iPos && currentPos < this.themeTops[i+1]) || (i === (length-1) && currentPos >= iPos),
       *  方案二:
       *    条件: 给themeTops最后添加一个很大的值, 用于和最后一个主题的top进行比较.
       */
        if (this.currentIndex !== i && positionY >= this.themeTops[i] && positionY < this.themeTops[i+1]) {
            this.currentIndex = i;
            this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
    },
    /**
     * 网络请求方法 
     */
    getD_Detail() {
      this.iid = this.$route.params.iid;
      getDetail(this.iid).then(res => {
        const data = res.data.result
        //获取详情图片
        this.topImages = data.itemInfo.topImages;
        //获取商品信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services);
        //获取店铺信息
        this.shop = new Shop(data.shopInfo);
        //获取商品信息
        this.detailInfo = data.detailInfo;
        //保存参数信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule);
        //获取评论信息
        if (data.rate.list) {
          this.commentInfo = data.rate.list[0];
        }
      })
    },
    getD_Recommend() {
      getRecommend().then((res) => {
        /* 获取推荐信息 */
        this.recommends = res.data.data.list
      })
    },
    mounted() {
      /* 监听item图片加载完成 */
      this.$bus.$on('detailItemImgLoad',() => {
        this.$refs.scroll.refresh()
        this.getThemeTops()
      })
    },
  }
}
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}
.detail-nav{
  position: relative;
  z-index: 9;
  background-color: #fff;
}
.content{
  height: calc(100% - 44px - 58px);
  overflow: hidden;
}
.back-top {
  position: fixed;
  right: 25px;
  bottom: 100px;
}
</style>